<!doctype html>
<html lang="en-us">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>OpenTTD</title>
    <style>
      body {
        font-family: Tahoma, Arial, Helvetica, sans-serif;
        font-size: 14px;
        margin: 0;
        padding: 0;
      }

      div.background {
        background-image: url("");
        border: 0px none;
        display: flex;
        height: 100%;
        position: absolute;
        width: 100%;
        z-index: 1;
      }

      div.overlay {
        height: 40px;
        margin: 0 auto;
        opacity: 0;
        pointer-events: none;
        position: absolute;
        text-align: center;
        transition: opacity 0.3s;
        width: 100%;
        z-index: 3;
      }
      div.overlay > div {
        pointer-events: none;
      }

      div.background > div, div.overlay > div {
        margin: auto;
      }

      #filesystem {
        background-color: #e00000;
        border: 1px solid #fc6458;
        color: #fcf880;
        display: none;
        outline: #a00000;
        padding: 0 4px;
        width: 600px;
      }

      #title, #message {
        background-color: #838383;
        border: 1px solid #a8a8a8;
        outline: 1px solid #626262;
        padding: 0 4px;
        min-width: 260px;
      }

      #box.error #title, #box.error #message {
        background-color: #e00000;
        border: 1px solid #fc6458;
        outline: #a00000;
      }
      #box.error #message {
        color: #fcf880;
      }

      #title {
        color: #fcfcfc;
        height: 20px;
        text-shadow: 1px 1px #101010;
      }
      #message {
        color: #101010;
        height: 54px;
        padding: 4px 4px;
      }

      canvas.emscripten {
        border: 0px none;
        height: 100%;
        position: absolute;
        width: 100%;
        z-index: 2;
      }
    </style>
  </head>
  <body>
    <div class="background">
      <div id="box">
        <div id="title">
          Loading ...
        </div>
        <div id="message">
        </div>
      </div>
    </div>
    <div class="overlay" id="overlay">
      <div id="filesystem">
        Warning: savegames are stored in the Indexed DB of your browser.<br/>Your browser can delete savegames without notice!
      </div>
    </div>
    <div>
      <canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()" tabindex=-1></canvas>
    </div>

    <script type='text/javascript'>
      var statusElement = document.getElementById('status');
      var progressElement = document.getElementById('progress');
      var spinnerElement = document.getElementById('spinner');

      var Module = {
        preRun: [],
        postRun: [],
        arguments: [],
        totalDependencies: 42,
        doneDependencies: 0,
        lastDependencies: 1,

        print: function(text) {
            if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
            console.log(text);
        },

        printErr: function(text) {
            if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
            console.error(text);
        },

        canvas: (function() {
          var canvas = document.getElementById('canvas');

          // As a default initial behavior, pop up an alert when webgl context is lost. To make your
          // application robust, you may want to override this behavior before shipping!
          // See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
          canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false);

          return canvas;
        })(),

        setStatus: function(text) {
          var m = text.match(/^([^(]+)\((\d+(\.\d+)?)\/(\d+)\)$/);

          if (m) {
            text = "(" + m[2] + " / " + m[4] + ") " + m[1];
          }

          document.getElementById("message").innerHTML = text;
        },

        monitorRunDependencies: function(left) {
          /* If it goes up, a new dependency was added; down means one is
           * removed. We only track the latter. */
          if (left < Module.lastDependencies) {
            Module.doneDependencies += 1;
          }
          Module.lastDependencies = left;

          total = Module.totalDependencies;
          doing = Module.doneDependencies + 1;
          if (doing > total) {
            doing = total;
          }

          document.getElementById("title").innerHTML = "(" + doing + " / " + total + ") Loading ...";
          document.getElementById("message").innerHTML = "Preparing game ...";
        },

        onExit: function() {
          document.getElementById("canvas").style.display = "none";

          document.getElementById("title").innerHTML = "Thank you for playing!";
          document.getElementById("message").innerHTML = "We hope you enjoyed OpenTTD!<br/><br/>Reload your browser to restart the game.";
        },

        onAbort: function() {
          document.getElementById("canvas").style.display = "none";

          document.getElementById("box").className = "error";
          document.getElementById("title").innerHTML = "Crash :(";
          document.getElementById("message").innerHTML = "The game crashed!<br/><br/>Please reload your browser to restart the game.";
        },

        onWarningFs: function() {
          document.getElementById("filesystem").style.display = "inline-block";
          document.getElementById("overlay").style.opacity = 1;
          setTimeout(function() {
            document.getElementById("overlay").style.opacity = 0;
            setTimeout(function() {
              document.getElementById("filesystem").style.display = "none";
            }, 300);
          }, 10000);
        }
      };

      window.onerror = function() {
        Module.onAbort();
      };
    </script>
    {{{ SCRIPT }}}
  </body>
</html>
